package com.vincent.kdp.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView.Adapter;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.vincent.kdp.R;
import com.vincent.kdp.ui.bean.response.MemberSearchOutput;
import com.vincent.kdp.ui.other.MemberSearchActivity;

import java.util.List;

public class MemberSearchAdapter extends Adapter<ViewHolder> {

    private static final int TYPE_ITEM = 0;
    private static final int TYPE_FOOTER = 1;
    private MemberSearchActivity context;
    private List<MemberSearchOutput.DataBean.ListBean> data;


    public MemberSearchAdapter(MemberSearchActivity context, List data) {
        this.context = context;
        this.data = data;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getItemCount() {
        return data.size() == 0 ? 0 : data.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(context).inflate(R.layout.activity_item_membersearch, parent,
                    false);
            return new ItemViewHolder(view);
        } else if (viewType == TYPE_FOOTER) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_foot, parent,
                    false);
            return new FootViewHolder(view);
        }
        return null;
    }


    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        Log.d("VIEW_HOLDER",position+"");
        if(position<data.size()){
            MemberSearchOutput.DataBean.ListBean c = data.get(position);
            if (holder instanceof ItemViewHolder) {
                ItemViewHolder itemViewHolder=(ItemViewHolder)holder;
                itemViewHolder.barCode.setText("商品条码："+c.getBarCode());
                itemViewHolder.money.setText("消费金额："+c.getMoney());
                itemViewHolder.consumeNum.setText("消费单号："+c.getConsumeNum());
                itemViewHolder.name.setText("用户名称："+c.getName());
                if (onItemClickListener != null) {
                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            int position = holder.getLayoutPosition();
                            onItemClickListener.onItemClick(holder.itemView, position);
                        }
                    });

                    holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View v) {
                            int position = holder.getLayoutPosition();
                            onItemClickListener.onItemLongClick(holder.itemView, position);
                            return false;
                        }
                    });
                }
            }
        }else{
            if(holder instanceof FootViewHolder){
                FootViewHolder footViewHolder=(FootViewHolder)holder;
                if(!context.hasNext){
                    footViewHolder.progressBar.setVisibility(View.INVISIBLE);
                    footViewHolder.textView.setText("没有更多数据");
                }else{
                    footViewHolder.progressBar.setVisibility(View.VISIBLE);
                    footViewHolder.textView.setText("加载中");
                }
            }
        }
    }


    static class ItemViewHolder extends ViewHolder {

        TextView consumeNum;
        TextView name;
        TextView barCode;
        TextView money;

        public ItemViewHolder(View view) {
            super(view);
            consumeNum = (TextView) view.findViewById(R.id.consume_num);
            name = (TextView) view.findViewById(R.id.name);
            barCode = (TextView) view.findViewById(R.id.bar_code);
            money = (TextView) view.findViewById(R.id.money);
        }
    }

    static class FootViewHolder extends ViewHolder {

        ProgressBar progressBar;
        TextView textView;

        public FootViewHolder(View view) {
            super(view);
            progressBar=(ProgressBar) view.findViewById(R.id.progressBar);
            textView=(TextView) view.findViewById(R.id.footText);
        }
    }
}